package com.example.date_save_test;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.example.date_save_test.util.FileUtil;
import com.example.date_save_test.util.ToastUtil;

import java.io.File;
import java.util.BitSet;

public class ImageWriteActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView ivContent;
    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_write);
        Log.d("NING","ImageWriteActivityOncreate");
        ivContent = findViewById(R.id.iv_content);
        findViewById(R.id.btn_downLoadImage).setOnClickListener(this);
        findViewById(R.id.btn_saveImage).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_downLoadImage:
                String fileName = System.currentTimeMillis() + ".jpeg";
                //获取当下App打私有下载目录
                path = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + File.separator + fileName;
                //从指定打资源文件中获取位图对象
                Bitmap b1 = BitmapFactory.decodeResource(getResources(),R.drawable.fj);
                FileUtil.saveImage(path,b1);
                ToastUtil.successShow(this,"保存成功");
                break;
            case R.id.btn_saveImage:
                Bitmap b2 = FileUtil.openImage(path);
                ivContent.setImageBitmap(b2);
                break;
        }
    }
}